package com.lab.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lab.pojo.Consumable;
import com.lab.service.ConsumableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 耗材管理
 */
@RestController
@RequestMapping("/consumable")
public class ConsumableController {
    @Resource
    ConsumableService consumableService;
    Map<String,Object> result=new HashMap<String,Object>();
    @RequestMapping(value = "/listByPage")
    public Map<String,Object> listByPage(Integer page, Integer rows){
        //设置分页参数
        page=(page-1)*rows;
        PageHelper.offsetPage(page, rows);
        //查询所有数据
        List<Consumable> list= consumableService.allConsumables();
        System.out.println(consumableService.allConsumables());
        //使用PageInfo封装查询结果
        PageInfo<Consumable> pageInfo=new PageInfo<Consumable>(list);
        //从PageInfo对象取出查询结果
        //总记录数
        long total=pageInfo.getTotal();
        //当前页数据列表
        List<Consumable> consumableList=pageInfo.getList();
        result.put("total", total);
        result.put("rows", consumableList);
        return result;
    }
    /**
     * 添加和修改
     * @param consumable
     * @return
     */
    @RequestMapping(value="/saveConsumable",method = RequestMethod.POST)
    public Map<String,Object> saveConsumable(Consumable consumable){
        try {
            consumableService.saveConsumable(consumable);
            result.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }

    /**
     * 数据回显
     * @param cid
     * @return
     */
    @RequestMapping(value="/findConsumableById",method = RequestMethod.GET)
    public Consumable findConsumableById(Long cid) {
        Consumable consumable= consumableService.findConsumableById(cid);
        return consumable;
    }

    /**
     *删除数据
     * @param cid
     * @return
     */
    @RequestMapping(value="/deleteDevice",method = RequestMethod.DELETE)
    public Map<String,Object> deleteConsumable(Long[] cid){
        try {
            consumableService.deleteConsumable(cid);
            result.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }
}
